package com.hm.cms.web;

import java.io.IOException;
import java.io.PrintWriter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.hm.cms.mongo.vo.CmsColumnVo;
import com.hm.cms.share.service.CmsColumnService;
import com.hm.cms.util.CmsConfigDef;
import com.hm.cms.web.su.BaseController;
import com.hm.common.annotation.AuthenticationAnnotation;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.common.CommonUtil;
import com.hm.common.model.ControllerResult;
import com.hm.common.model.PageInfo;
import com.hm.common.util.CommonDef.OperationStatus;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 下午2:41:00 2016年8月9日
 * @version 1.0
 * @describe
 */
@ResponseBody
@RestController
@RequestMapping(value = CmsConfigDef.Api.Rest.Cms.REST_CMS_COLUMN)
public class CmsColumnController extends BaseController {

	public static final String swaggerTag = "栏目信息";

	@Autowired
	private CmsColumnService cmsColumnService;

	@AuthenticationAnnotation
	@RequestMapping(value = "/{columnId}", method = RequestMethod.GET)
	@ApiOperation(value = "获取栏目信息基本信息", httpMethod = "GET", notes = "获取栏目信息基本信息通过ID", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取栏目信息基本信息", content = "获取栏目信息基本信息通过ID")
	public ResponseEntity<ControllerResult<CmsColumnVo>> getCmsColumn(
			@ApiParam(required = true, name = "columnId", value = "栏目信息ID") @PathVariable String columnId) {
		try {
			if (CommonUtil.isEmpty(columnId)) {
				return ControllerResult.failed(OperationStatus.LOAD_FAIL + ",基本信息ID不能为空");
			}
			return ControllerResult.success(cmsColumnService.getCmsColumn(columnId), OperationStatus.LOAD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(value = "添加栏目信息", httpMethod = "POST", notes = "添加栏目信息-基本信息", tags = swaggerTag)
	@RequestPlanAnnotation(title = "添加栏目信息基本信息", content = "添加栏目信息-基本信息")
	public ResponseEntity<ControllerResult<String>> addCmsColumn(
			@ApiParam(required = true, name = "body", value = "栏目信息-基本信息") @RequestBody(required = true) CmsColumnVo cmsColumnVo) {
		try {
			if (CommonUtil.isEmpty(cmsColumnVo.getName())) {
				return ControllerResult.failed(OperationStatus.ADD_FAIL + ",基本信息不完整");
			}
			return ControllerResult.success(cmsColumnService.addCmsColumn(cmsColumnVo, currentUserId()),
					OperationStatus.ADD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{columnId}", method = RequestMethod.PUT)
	@ApiOperation(value = "修改栏目信息", httpMethod = "PUT", notes = "修改栏目信息-基本信息", tags = swaggerTag)
	@RequestPlanAnnotation(title = "修改栏目信息", content = "修改栏目信息-基本信息")
	public ResponseEntity<ControllerResult<String>> updateCmsColumn(
			@ApiParam(required = true, name = "body", value = "栏目信息-基本信息") @RequestBody(required = true) CmsColumnVo cmsColumnVo,
			@ApiParam(required = true, name = "columnId", value = "栏目信息ID") @PathVariable String columnId) {
		try {
			if (CommonUtil.isEmpty(cmsColumnVo.getName(), columnId)) {
				return ControllerResult.failed(OperationStatus.UPDATE_FAIL + ",基本信息不完整");
			}
			cmsColumnVo.setId(columnId);
			return ControllerResult.success(cmsColumnService.updateCmsColumn(cmsColumnVo, currentUserId()),
					OperationStatus.UPDATE_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{columnId}", method = RequestMethod.DELETE)
	@ApiOperation(value = "删除栏目信息", httpMethod = "DELETE", notes = "删除栏目信息-基本信息", tags = swaggerTag)
	@RequestPlanAnnotation(title = "删除栏目信息", content = "删除栏目信息-基本信息")
	public ResponseEntity<ControllerResult<String>> deleteCmsColumn(
			@ApiParam(required = true, name = "columnId", value = "栏目信息ID") @PathVariable String columnId) {
		try {
			if (CommonUtil.isEmpty(columnId)) {
				return ControllerResult.failed(OperationStatus.DELELETE_FAIL + ",栏目信息ID不能为空");
			}
			CmsColumnVo cmsColumnVo = new CmsColumnVo();
			{
				cmsColumnVo.setId(columnId);
			}
			return ControllerResult.success(cmsColumnService.deleteCmsColumn(cmsColumnVo, currentUserId()),
					OperationStatus.DELELETE_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(value = "获取栏目信息", httpMethod = "GET", notes = "获取栏目信息-基本信息数据", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取栏目信息", content = "获取栏目信息-基本信息数据")
	public ResponseEntity<ControllerResult<PageInfo<CmsColumnVo>>> queryCmsColumn(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,小于1则默认返回第一页)") @PathVariable("pageNumber") Integer pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") Integer pageSize,
			@ApiParam(required = true, name = "weibsite", value = "站点编号") @RequestParam(required = true) String weibsite,
			@ApiParam(required = true, name = "parenId", value = "上级栏目编号") @RequestParam(required = true) String parenId) {
		try {
			if (CommonUtil.isEmpty(pageNumber, pageSize)) {
				return ControllerResult.failed(OperationStatus.LOAD_FAIL + ",请求参数错误");
			}
			return ControllerResult.success(cmsColumnService.queryCmsColumn(pageNumber, pageSize, weibsite, parenId),
					OperationStatus.LOAD_SUCCESSFUL);
		} catch (Exception e) {
			return ControllerResult.failed(e);
		}
	}

	@AuthenticationAnnotation
	@RequestMapping(value = "/{weibsite}/childs", method = RequestMethod.GET)
	@ApiOperation(value = "获取栏目树", httpMethod = "GET", notes = "根据栏目id获取栏目树", tags = swaggerTag)
	@RequestPlanAnnotation(title = "获取栏目树", content = "根据栏目id获取栏目树")
	public void loadChilds(@ApiParam(required = true, name = "weibsite", value = "站点编号") @PathVariable String weibsite,
			@ApiParam(required = false, name = "parenId", value = "栏目编号ID") @RequestParam(required = false) String parenId) {
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json");

			PrintWriter writer = response.getWriter();
			writer.print(JSONArray.toJSONString(cmsColumnService.childsById(parenId, weibsite)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
